package com.engine.avic.duss.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.engine.avic.duss.util.utils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-10
 * @Description: ${description}
 * @ModifyLog:
 **/

public class WeaMeetPanelWeb {
    /**
     * @Description: 获取当前服务器时间
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getDatetime")
    @Produces({MediaType.TEXT_PLAIN})
    public String getDatetime(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            rs.executeQuery( "  select convert(varchar(100),GETDATE(),120) as datetime,operatetime from uf_hydpsx");
            rs.next();
            String datetime = Util.null2String( rs.getString( "datetime" ) );
            String operatetime = Util.null2String( rs.getString( "operatetime" ) );
            apidatas.put( "datetime", datetime );
            apidatas.put( "operatetime", operatetime );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "error_message", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }

    /**
     * @Description: 获取数据看板
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/getMeetInfo")
    @Produces({MediaType.TEXT_PLAIN})
    public String getMeetInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        BaseBean baseBean = new BaseBean();
        baseBean.writeLog( ">>>>>getMeetInfo" );
        JSONArray jsonArray = new JSONArray();
        RecordSet rs = new RecordSet();
        try {
            rs.executeQuery( "select top 4 xh,ycmc,zrbm,hbry,lxry,yjsc from uf_hydp where dqzt !=1 order by xh" );
            int num = 1;
            while (rs.next()) {
                JSONObject jsonData = new JSONObject();
                String zrbm = Util.null2String(rs.getString( "zrbm" ));
                String hbry = Util.null2String(rs.getString( "hbry" ));
                String lxry = Util.null2String(rs.getString( "lxry" ));
                jsonData.put( "index", num);
                jsonData.put( "name", Util.null2String( rs.getString( "ycmc" ) ) );
                jsonData.put("reportmembers",utils.getUserName(hbry));
                jsonData.put("hrmmembers",utils.getUserName(lxry));
                jsonData.put("depart",utils.getDepartName(zrbm));
                jsonData.put( "times", Util.null2String( rs.getString( "yjsc" ) ) );
                jsonData.put( "status", utils.getNumber( num ) );
                jsonArray.add( jsonData );
                num++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray.toJSONString();
    }

    /**
     * @Description: 修改状态
     * @Param: [request, response]
     * @return: java.lang.String
     * @Author: Dusongsong
     * @Date: 2021/3/10
     */
    @GET
    @Path("/editmeetStatus")
    @Produces({MediaType.TEXT_PLAIN})
    public String editmeetStatus(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        Map<String, Object> apidatas = new HashMap<String, Object>();
        try {
            RecordSet rs = new RecordSet();
            String id = Util.null2String( request.getParameter( "id" ) );
            boolean flag = rs.executeUpdate( "update uf_hydp set dqzt=1 where id = ? ", id );
            rs.executeUpdate( "update uf_hydpsx set operatetime =(select convert(varchar(100),dateadd(SECOND,+5,GETDATE()),120))" );
            apidatas.put( "flag", flag );
            apidatas.put( "api_status", true );
        } catch (Exception e) {
            e.printStackTrace();
            apidatas.put( "api_status", false );
            apidatas.put( "error_message", "请求接口异常" );
        }
        return JSONObject.toJSONString( apidatas );
    }
}
